//
//  HMBaseViewController.swift
//  iOSWeibo12
//
//  Created by Admin on 16/9/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMBaseViewController: UIViewController {

    /// 用户是否登入标志
    var userLogin: Bool = HMUserAccountViewModel.shared.userLogin
    
    override func loadView() {
        if userLogin {
            // 使用talbeView
            self.view = tableView
            
            tableView.delegate = self
            tableView.dataSource = self
        } else {
            setupVisitorView()
        }
    }
    
    private func setupVisitorView() {
        // 使用访客视图
        self.view = visitorView
        
        // 判断当前是哪个控制器
        if self is HMHomeViewController {
            visitorView.startRotationAnimation()
        } else if self is HMMessageViewController {
            visitorView.setupVisitorInfo(imageName: "visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        } else if self is HMDiscoveryViewController {
            visitorView.setupVisitorInfo(imageName: "visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        } else if self is HMProfileViewController {
            visitorView.setupVisitorInfo(imageName: "visitordiscover_image_profile", message: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
        
        visitorView.vistorViewDelegate = self
        
        // 添加导航栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.plain, target: self, action: #selector(vistorViewRegister))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.plain, target: self, action: #selector(vistorViewLogin))
    }
    
    // MARK: - 懒加载
    /// 访客视图
    private lazy var visitorView: HMVisitorView = HMVisitorView()
    
    /// tableView
    lazy var tableView: UITableView = UITableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
}

// MARK: - CZBaseTableViewController 扩展,实现 CZVistorViewDelegate 协议
// 扩展,和oc category类似, estension 可以实现协议, 方便代码统一管理
extension HMBaseViewController: HMVisitorViewDelegate {
    
    // 点击注册
    func vistorViewRegister() {
        print("HMBaseViewController: vistorViewRegister")
    }
    
    // 点击登录
    func vistorViewLogin() {
        let oauthVC = HMOauthViewController()

        present(UINavigationController(rootViewController: oauthVC), animated: true, completion: nil)
    }
}

// MARK: - tableView数据源
extension HMBaseViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    }
}

// MARK: - tableView代理
extension HMBaseViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 0
    }
}
